<?php
namespace App\Admin\Controllers\Base;

use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

use App\Models\Base\Category as ThisModel;
use Encore\Admin\Auth\Database\Administrator;

class CategoryController extends AdminController
{
    protected $title = '分类管理';

    protected function grid()
    {
        $types = ThisModel::getTypes();
        $states = ThisModel::getShowHide();
        $users = Administrator::pluck('name', 'id')->toArray();

        $grid = new Grid(new ThisModel);

        $grid->model()->orderBy('id', 'desc');

        $grid->column('id', __('ID'))->sortable();
        $grid->column('type', __('Type'))->using($types);
        $grid->column('title', __('Title'));
        $grid->column('number', __('Number'));
        $grid->column('slug', __('Slug'));
        $grid->column('state', __('State'))->using($states);
        $grid->column('sort', __('Sort'));
        $grid->column('remark', __('Remark'));

        $grid->column('created_by', __('Created by'))->display(function ($val) use ($users) {
            return $users[$val] ?? __('Unknown');
        });
        $grid->column('created_at', __('Created at'));
        $grid->column('updated_at', __('Updated at'));

        $grid->filter(function ($filter) use ($types, $states) {
            $filter->disableIdFilter();
            $filter->column(1/3, function ($filter) {
                $filter->equal('id', __('ID'));
                $filter->equal('slug', __('Slug'));
            });
            $filter->column(1/3, function ($filter) use ($types) {
                $filter->equal('type', __('Type'))->select($types);
                $filter->equal('number', __('Number'));
            });
            $filter->column(1/3, function ($filter) use ($states) {
                $filter->equal('state', __('State'))->select($states);
                $filter->between('created_at', __('Created at'))->date();
            });
        });

        return $grid;
    }
    protected function detail($id)
    {
        $show = new Show(ThisModel::findOrFail($id));

        $show->field('id', __('ID'));
        $show->field('created_at', __('Created at'));
        $show->field('updated_at', __('Updated at'));

        return $show;
    }
    protected function form()
    {
        $types = ThisModel::getTypes();
        $states = ThisModel::getShowHide();

        $form = new Form(new ThisModel);

        // $form->display('id', __('ID'));
        $form->text('id', __('ID'));
        $form->select('type', __('Type'))->options($types);
        $form->text('title', __('Title'));
        $form->text('number', __('Number'));
        $form->text('slug', __('Slug'));
        $form->select('state', __('State'))->options($states);
        $form->number('sort', __('Sort'))->min(0)->max(255);
        $form->text('remark', __('Remark'));
        // $form->display('created_at', __('Created At'));
        // $form->display('updated_at', __('Updated At'));

        return $form;
    }
}
